package com.yz.smartpos.yztester.ui;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.app.Fragment;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.yz.smartpos.yztester.R;
import com.yz.smartpos.yztester.util.TestCase;
import com.yz.smartpos.yztester.util.TesterConfig;
import com.yz.smartpos.yztester.util.TesterState;
import com.yz.smartpos.yztester.util.Utils;
import com.yz.smartpos.yztester.util.YzTestRunner;
import com.yz.smartpos.yztester.apifuntest.tests.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;

/**
 */

public class OperateFragment extends Fragment {

    public static ExpandableListView expandableListView;
    public static MyExpandableListAdapter adapter;
    private static String TAG = "OperateFragment";
    public DrawerLayout drawer_layout;
    public FrameLayout frag_left;
    public static OperateFragment newInstance() {
        OperateFragment fragment = new OperateFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    public OperateFragment() {
    }

    public static Handler mhandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0:
                    Log.i(TAG, String.format("update adapter:%d",msg.arg1));
                    adapter.notifyDataSetChanged();//更新数据
                    expandableListView.collapseGroup((Integer) msg.arg1);
                    expandableListView.expandGroup((Integer) msg.arg1);
                    break;
            }
            return true;
        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        Log.i(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_operate, container, false);
        drawer_layout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        frag_left = (FrameLayout) getActivity().findViewById(R.id.operate);

        init(view);
        TesterConfig.initDir(getActivity());
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
//        TesterConfig.tearDir();
    }
    private void init(View view) {
        TesterState.caseList_suc = new HashSet<TestCase>();
        TesterState.caseList_fail = new HashSet<TestCase>();
        TesterState.caseList_unsup = new HashSet<TestCase>();
        TesterState.caseList_runned = new HashSet<TestCase>();
        TesterState.caseList_auto = new HashSet<TestCase>();
        TesterState.caseList_auto_rf = new HashSet<TestCase>();

        TesterState.interfaceList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.inter)));
        TesterState.interClassList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.inter_class)));
        Log.i(TAG, "interfaceList" + TesterState.interfaceList);
        TesterState.dataSet = new HashMap<>();
        TesterState.dataSet.put(TesterState.interfaceList.get(0), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(1), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(2), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(3), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(4), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(5), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(6), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(7), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(8), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(9), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(10), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(11), SerialPortTest.testcaseList);
        TesterState.dataSet.put(TesterState.interfaceList.get(12), SerialPortTest.testcaseList);

        expandableListView = (ExpandableListView) view.findViewById(R.id.interface_list);
        expandableListView.setGroupIndicator(null);
        expandableListView.setOnGroupClickListener((parent, v, groupPosition, id) -> false);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, final int groupPosition, final int childPosition, long id) {
                if(TesterState.getRunState() == TesterState.TEST_RUNNING){
                    Utils.showToast("Testing", Toast.LENGTH_LONG);
                    Log.i(TAG, "Testing...");
                }
                else{
//                    drawer_layout.closeDrawer(frag_left);
                    TestCase inter = TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition)).get(childPosition);
                    String caseName = inter.getCaseName();
                    ArrayList<TestCase> caselist = new ArrayList<TestCase>();

                    if(caseName == "RunAllAuto"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAutoFail"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO && TesterState.caseList_fail.contains(testcase)){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAutoNotRun"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO && !TesterState.caseList_runned.contains(testcase)){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllDataNetwork"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO && testcase.getCaseName().startsWith("DataNetwork_")){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAuto_nocard"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO_nocard){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAutoNotRun_scan"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if(testcase.getRunType() == TestCase.AUTO_scan && !TesterState.caseList_runned.contains(testcase)){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAutoNotRun_paper_rf"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if((testcase.getRunType() == TestCase.AUTO_paper || testcase.getRunType() == TestCase.AUTO_rf) && !TesterState.caseList_runned.contains(testcase)){
                                caselist.add(testcase);
                            }
                        }
                    }else if(caseName == "RunAllAutoNotRun_paper_rf_scan"){
                        for(TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(groupPosition))){
                            if((testcase.getRunType() == TestCase.AUTO_paper || testcase.getRunType() == TestCase.AUTO_rf || testcase.getRunType() == TestCase.AUTO_scan) && !TesterState.caseList_runned.contains(testcase)){
                                caselist.add(testcase);
                            }
                        }
                    }
                    else if(caseName == "RunAllAuto_set"){
                        if(TesterState.caseList_auto == null) {
                            for(List<TestCase> testCaseList : TesterState.dataSet.values()) {
                                for(TestCase testcase : testCaseList){
                                    if(testcase.getRunType() == TestCase.AUTO){
                                        TesterState.caseList_auto.add(testcase);
                                    }
                                }
                            }
                        }
                        caselist.addAll(TesterState.caseList_auto);
                    } else if(caseName == "RunAllAutoNotRun_set"){
                        if(TesterState.caseList_auto == null) {
                            for(List<TestCase> testCaseList : TesterState.dataSet.values()) {
                                for(TestCase testcase : testCaseList){
                                    if(testcase.getRunType() == TestCase.AUTO){
                                        TesterState.caseList_auto.add(testcase);
                                    }
                                }
                            }
                        }
                        for(TestCase testcase : TesterState.caseList_auto){
                            if(!TesterState.caseList_runned.contains(testcase))
                                caselist.add(testcase);
                        }
                    }
                    else {
                        TesterState.setRunState(TesterState.TEST_RUNNING);
                        YzTestRunner mYzTestRunner = new YzTestRunner(inter);
                        Thread t = new Thread(mYzTestRunner);
                        t.start();
                    }

                    if(!caselist.isEmpty()){
                        TesterState.setRunState(TesterState.TEST_RUNNING);
                        YzTestRunner mYzTestRunner = new YzTestRunner(caselist, 0);
                        Thread t = new Thread(mYzTestRunner);
                        t.start();
                    }

                    TesterState.setRunState(TesterState.TEST_IDIE);
                }
                Log.i("Frag", "end onChildClick");
                return false;
            }
        });
        adapter = new MyExpandableListAdapter(getActivity(), TesterState.interfaceList, TesterState.dataSet);
        expandableListView.setAdapter(adapter);
    }
}
